import os
from PIL import Image, ImageDraw


# 检查给定的数组是否包含模式pattern
def contains_pattern(sub_arr, pattern):
    return len(sub_arr) >= len(pattern) and sub_arr[:len(pattern)] == pattern


def split_array_by_pattern(arr, pattern):
    result = []
    current_subarray = []
    i = 0
    while i < len(arr):
        if contains_pattern(arr[i:i + len(pattern)], pattern):
            if current_subarray:  # 如果current_subarray不为空，则添加到result中
                result.append(current_subarray)
            current_subarray = []  # 重置current_subarray为新的子数组
            i += len(pattern)  # 跳过模式pattern对应的元素
        else:
            current_subarray.append(arr[i])
            i += 1
    if current_subarray:  # 添加最后一部分，如果有的话
        result.append(current_subarray)
    return result


def drawImg(coors, save_path, is_debug=False):
    img = Image.new('RGB', (466, 466), 'white')
    draw = ImageDraw.Draw(img)
    pattern = [-1, 0, -1, -1]
    coordinates_list = split_array_by_pattern(coors, pattern)

    for coordinate in coordinates_list:
        # 将坐标数据转换为元组列表
        points = list(zip(coordinate[::2], coordinate[1::2]))
        # 根据坐标点连线
        draw.line(points, fill='black', width=5)

    if is_debug:
        # 调试显示图像
        img.show()
    # 或者保存图像
    img.save(save_path)


if __name__ == "__main__":
    coordinates = []
    a = [181, 44, 181, 44, 180, 44, 178, 44, 174, 44, 172, 44, 170, 44, 168, 44, 166, 44, 164, 44, 162, 46, 160, 46,
         159, 48, 157, 48, 155, 50, 153, 52, 149, 52, 147, 54, 145, 56, 143, 58, 141, 59, 138, 61, 134, 63, 132, 67,
         128, 71, 128, 73, 128, 75, 124, 79, 118, 84, 113, 94, 103, 107, 103, 109, 101, 113, 99, 115, 99, 117, 99, 119,
         97, 121, 97, 124, 97, 130, 99, 136, 101, 143, 103, 147, 103, 151, 107, 157, 109, 161, 109, 163, 111, 163, 111,
         164, 113, 164, 115, 164, 117, 164, 118, 164, 122, 166, 130, 170, 145, 178, 153, 182, 159, 185, 164, 187, 166,
         187, 168, 187, 170, 187, 172, 187, 176, 187, 183, 187, 193, 187, 201, 187, 204, 187, 210, 187, 214, 187, 218,
         187, 220, 187, 222, 187, 225, 185, 227, 184, 231, 182, 235, 180, 237, 178, 241, 176, 246, 172, 250, 170, 254,
         168, 258, 166, 260, 164, 264, 159, 273, 147, 288, 128, 296, 119, 302, 107, 307, 100, 311, 94, 315, 90, 317, 86,
         319, 84, 319, 82, 315, 79, 311, 73, 307, 65, 304, 61, 300, 56, 298, 52]
    b = [294, 50, 292, 48, 290, 46, 290, 44, 288, 44, 286, 44, 285, 42, 283, 42, 281, 42, 277, 42, 273, 42, 271, 42,
         267, 40, 265, 40, 262, 40, 258, 40, 252, 40, 248, 38, 244, 38, 241, 38, 237, 38, 231, 38, 225, 40, 220, 40,
         214, 42, 208, 42, 204, 42, 202, 42, 201, 42, 199, 44, 197, 44, 197, 42, 195, 42, 193, 42, 189, 42, 187, 42,
         185, 42, 183, 42, 183, 44, 183, 44, -1, 0, -1, -1, 136, 105, 136, 105, 136, 103, 138, 101, 138, 100, 139, 98,
         141, 96, 143, 94, 145, 92, 147, 90, 149, 90, 149, 88, 151, 88, 151, 86, 153, 86, 155, 84, 157, 84, 157, 82,
         159, 82, 162, 82, 164, 80, 166, 80, 168, 80, 170, 80, 172, 80, 174, 80, 176, 80, 176, 82, 178, 82, 180, 84,
         181, 86, 181, 88, 183, 90, 185, 92, 185, 94, 187, 96, 187, 98, 187, 100, 187, 100, -1, 0, -1, -1, 212, 100,
         212, 100, 212, 98, 212, 96, 212, 94, 214, 90, 214, 88, 216, 84, 218, 82, 220, 80, 222, 79, 223, 79, 223, 77,
         225, 77, 227, 75, 229, 75, 231, 75]
    c = [233, 75, 235, 75, 237, 75, 239, 75, 241, 75, 243, 75, 244, 75, 246, 75, 248, 75, 250, 75, 250, 77, 250, 79,
         252, 79, 252, 80, 252, 80, -1, 0, -1, -1, 176, 136, 176, 136, 176, 138, 178, 140, 180, 142, 181, 143, 183, 143,
         185, 145, 187, 145, 191, 147, 193, 149, 195, 149, 197, 149, 199, 151, 201, 151, 202, 151, 202, 153, 204, 153,
         206, 153, 208, 153, 210, 153, 212, 153, 214, 151, 216, 147, 218, 145, 220, 142, 222, 140, 223, 138, 223, 136,
         223, 134, 223, 134, -1, 0, -1, -1, 187, 199, 187, 201, 187, 205, 181, 212, 180, 220, 180, 222, 178, 226, 178,
         227, 178, 229, 176, 237, 176, 239, 176, 241, 176, 243, 176, 245, 176, 247, 176, 248, 176, 250, 176, 254, 176,
         256, 176, 258, 176, 260, 176, 262, 176, 264, 178, 264, 178, 266, 180, 268, 181, 270, 185, 271, 187, 273, 191,
         275, 193, 275, 197, 277, 199, 277, 202, 279, 204, 279, 208, 279, 212, 279, 216, 279, 220, 279, 225, 279, 227,
         279, 231, 279, 233, 279, 237, 277, 239, 275, 243, 273, 244, 271, 248, 270, 250, 268, 252, 266]
    d = [256, 264, 258, 262, 260, 260, 260, 258, 262, 250, 262, 245, 262, 237, 262, 231, 262, 224, 262, 218, 262, 214,
         262, 210, 262, 206, 262, 205, 262, 201, 262, 199, 262, 195, 260, 193, 260, 191, 258, 189, 258, 187, 256, 187,
         256, 185, 254, 185, 254, 185, -1, 0, -1, -1, 181, 220, 181, 220, 180, 220, 178, 220, 176, 220, 174, 220, 170,
         220, 168, 220, 164, 222, 160, 222, 157, 224, 155, 227, 153, 227, 151, 229, 147, 231, 145, 233, 143, 233, 143,
         235, 141, 235, 141, 237, 139, 237, 139, 239, 138, 239, 136, 239, 134, 241, 132, 243, 130, 243, 130, 243, -1, 0,
         -1, -1, 260, 210, 260, 210, 260, 212, 262, 212, 264, 212, 267, 212, 273, 212, 283, 212, 306, 212, 313, 212,
         317, 214, 319, 214, 321, 214, 323, 214, 323, 214, -1, 0, -1, -1, 208, 273, 206, 273, 204, 277, 202, 279, 201,
         281, 201, 283, 199, 285, 197, 289, 197, 291, 195, 292, 195, 294, 193, 296, 193, 298, 191, 300, 189, 302, 189,
         304, 187, 304, 187, 306, 187, 306, -1, 0, -1, -1, 235, 277, 235, 277, 237, 277, 243, 277, 246, 279]
    e = [250, 279, 254, 281, 256, 281, 258, 281, 260, 283, 262, 283, 264, 285, 264, 287, 265, 287, 265, 289, 267, 289,
         267, 291, 269, 292, 269, 294, 271, 296, 271, 298, 271, 300, 271, 300, -1, 0, -1, -1]
    file_path = os.path.join("../static/files", "line_drawing.png")
    coordinates.extend(a)
    coordinates.extend(b)
    coordinates.extend(c)
    coordinates.extend(d)
    coordinates.extend(e)
    drawImg(coordinates, file_path, is_debug=True)
